package cn.yuanqiao.quartz.task;

import cn.yuanqiao.archive.appManager.domain.TenantCategory;
import cn.yuanqiao.archive.appManager.domain.TenantYqdaDocument;
import cn.yuanqiao.archive.appManager.mapper.*;
import cn.yuanqiao.archive.guangpanguanli.service.ArchiveService;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.quartz.util.MethodDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Files;
import java.util.List;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

@Component("deleteTask")
public class DeleteTask {
    private static final Logger log = LoggerFactory.getLogger(DeleteTask.class);

    @Autowired
    private TenantCategoryMapper categoryMapper;
    @Autowired
    private TenantYqdaDocumentMapper documentMapper;
    @Autowired
    private InitArchiveMapper initArchiveMapper;
    @Autowired
    private ArchiveService archiveService;
    @Autowired
    private TenantYqdaMapper yqdaMapper;

    @MethodDescription("删除文件任务，arg0需要传入tenantCode")
    public void del(String categoryId,String tenantCode){
        log.info("categoryId="+categoryId);
        log.info("tenantCode="+tenantCode);
        if(StringUtils.isEmpty(categoryId) || StringUtils.isEmpty(tenantCode)){
            log.info("参数错误!");
        }else {
            TenantCategory category = categoryMapper.selectTenantCategoryByID(Long.valueOf(categoryId),tenantCode + TenantCategory.BIGTABLENAME);
            log.info(category.toString());
            if(category == null){
                log.info("category is null");
            }else {
                //先删除电子文件
                TenantYqdaDocument docSearch = new TenantYqdaDocument();
                docSearch.setTableName(category.getEfileTableName());
                docSearch.setIsDel(1); //查询已删除
                List<TenantYqdaDocument> documentList = documentMapper.getDocumentListAll(docSearch);
                log.info("总计{}电子件",documentList.size());
                for(TenantYqdaDocument doc:documentList){
                    log.info("docId="+doc.getDocId());
                    File file = new File(doc.getDocMnt()+doc.getPath());
                    log.info("source="+file.getAbsolutePath());
                    if(file.exists()){
                        File toFile = new File("/data1/del/"+doc.getPath());
                        log.info("target="+toFile.getAbsolutePath());
                        if(!toFile.getParentFile().exists()){
                            toFile.getParentFile().mkdirs();
                        }
                        try {
                            Files.move(file.toPath(),toFile.toPath(),REPLACE_EXISTING);
                            log.info("success");
                        }catch (Exception e1){
                            log.error(e1.getMessage(),e1);
                        }
                    }else {
                        log.info("does not exist");
                    }
                    doc.setTableName(category.getEfileTableName()+"_BACK");
                    doc.setDocMnt("/data1/del/");
                    //删除和插入备份表
                    archiveService.delFile(category,doc);
                }








                /*int e = initArchiveMapper.isExistTable(category.getEfileTableName()+"_BACK");
                if(e == 0){
                    log.info(category.getEfileTableName()+"_BACK is null");
                    return;
                }
                int f = initArchiveMapper.isExistTable(category.getFileTableName()+"_BACK");
                if(f == 0){
                    log.info(category.getFileTableName()+"_BACK is null");
                    return;
                }
                if(category.getType() == 3 || category.getType() == 1){
                    int v = initArchiveMapper.isExistTable(category.getFolderTableName()+"_BACK");
                    if(v == 0){
                        log.info(category.getFolderTableName()+"_BACK is null");
                        return;
                    }
                }
                if(category.getType() == 3){
                    int x = initArchiveMapper.isExistTable(category.getItemTableName()+"_BACK");
                    if(x == 0){
                        log.info(category.getItemTableName()+"_BACK is null");
                        return;
                    }
                }
                File fileMnt = new File("/data1/del");
                if(!fileMnt.exists()){
                    fileMnt.mkdirs();
                }
                //先删除电子文件
                TenantYqdaDocument docSearch = new TenantYqdaDocument();
                docSearch.setTableName(category.getEfileTableName());
                docSearch.setIsDel(1); //查询已删除
                List<TenantYqdaDocument> documentList = documentMapper.getDocumentListAll(docSearch);
                log.info("总计{}电子件",documentList.size());
                for(TenantYqdaDocument doc:documentList){
                    File file = new File(doc.getDocMnt()+doc.getPath());
                    log.info("source="+file.getAbsolutePath());
                    if(file.exists()){
                        File toFile = new File("/data1/del/"+doc.getPath());
                        log.info("target="+toFile.getAbsolutePath());
                        if(!toFile.getParentFile().exists()){
                            toFile.getParentFile().mkdirs();
                        }
                        try {
                            Files.move(file.toPath(),toFile.toPath(),REPLACE_EXISTING);
                            log.info("success");
                        }catch (Exception e1){
                            log.error(e1.getMessage(),e1);
                        }
                    }
                    doc.setTableName(category.getEfileTableName()+"_BACK");
                    doc.setDocMnt("/data1/del/");
                    //删除和插入备份表
                    archiveService.delFile(category,doc);
                }

                //删除件
                List<Map<String,Object>> yqdaList = yqdaMapper.getYqdaAll(category.getFileTableName()," where IS_DEL = 1");
                log.info("总计{}件",yqdaList.size());
                for(Map<String,Object> yqda:yqdaList){
                    archiveService.delYqda(yqda,tenantCode,category);
                }

                if(category.getType() == 3 || category.getType() == 1){
                    //开始删除卷
                    List<Map<String,Object>> folderList = yqdaMapper.getYqdaAll(category.getFolderTableName()," where IS_DEL = 1");
                    log.info("总计{}卷",folderList.size());
                    for(Map<String,Object> folder:folderList){
                        archiveService.delFolder(folder,tenantCode,category);
                    }
                }
                if(category.getType() == 3){
                    //开始删除项目
                    List<Map<String,Object>> itemList = yqdaMapper.getYqdaAll(category.getItemTableName()," where IS_DEL = 1");
                    log.info("总计{}项目",itemList.size());
                    for(Map<String,Object> item:itemList){
                        archiveService.delItem(item,tenantCode,category);
                    }
                }
                log.info("删除完成");*/
            }
        }
    }
}
